package com.syq.event.listener;

import com.syq.dao.UserloginMapper;
import com.syq.event.LoginEvent;
import com.syq.pojo.SysLogininfor;
import com.syq.pojo.SysUserOnline;
import com.syq.service.SysUserOnlineService;
import com.syq.service.SysUserService;
import com.syq.service.UserloginService;
import com.syq.utils.RequestUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author Sun Yueqi
 * @version 1.00
 * @time 2022/5/26 10:56
 */
@Component
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class LoginListener {
    private ThreadPoolExecutor threadPoolExecutor;

    private UserloginService userloginService;

    private SysUserService sysUserService;

    private SysUserOnlineService sysUserOnlineService;

    @Autowired
    public void setSysUserOnlineService(SysUserOnlineService sysUserOnlineService) {
        this.sysUserOnlineService = sysUserOnlineService;
    }

    @Autowired
    public void setSysUserService(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    @Autowired
    public void setUserloginService(UserloginService userloginService) {
        this.userloginService = userloginService;
    }

    @Autowired
    public void setThreadPoolExecutor(ThreadPoolExecutor threadPoolExecutor) {
        this.threadPoolExecutor = threadPoolExecutor;
    }

    @EventListener
   public void onEvent(LoginEvent event){
       String username = event.getUsername();
       ServletRequestAttributes servletRequestAttributes= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
       HttpServletRequest request = servletRequestAttributes.getRequest();
       //操作系统
       Map<String, String> osAndBrowserInfo = RequestUtil.getOsAndBrowserInfo(request);
       //ip
       String ip = RequestUtil.getRemoteHost(request);

        Date processDate = new Date();
        CompletableFuture.runAsync(()->{
           //登陆地区
           String location = RequestUtil.getLocationByIp(ip);
           SysLogininfor sysLogininfor = new SysLogininfor();

           sysLogininfor.setLoginName(username);
           sysLogininfor.setBrowser(osAndBrowserInfo.get("browser"));
           sysLogininfor.setLoginTime(processDate);
           sysLogininfor.setIpaddr(ip);
           sysLogininfor.setLoginLocation(location);

           switch (event.getType()){
               case SUCCESS :
                   sysLogininfor.setMsg("登陆成功");
                   sysLogininfor.setStatus("1");
                   break;
               case FAIL:
                   sysLogininfor.setMsg("登陆失败");
                   sysLogininfor.setStatus("0");
                   break;
           }
           SysUserOnline sysUserOnline=new SysUserOnline();
           BeanUtils.copyProperties(sysLogininfor,sysUserOnline);
           sysUserOnline.setSessionId(request.getSession().getId());
           userloginService.addLoginLog(sysLogininfor);
           sysUserOnlineService.addOrUpdateOnline(sysUserOnline);
       },threadPoolExecutor);

        CompletableFuture.runAsync(()->{
            switch (event.getType()) {
                case SUCCESS :
                sysUserService.updateLogin(username, processDate, ip);
                break;
            }
        },threadPoolExecutor);
   }
}

